
lobstercommon.name = "lobsterh5";
var formid = lobstercommon.GetUrlParam("formid");
var backurl = decodeURIComponent(lobstercommon.GetUrlParam("backurl") || "");

$(document).ready(function () {

    var md = new MobileDetect(window.navigator.userAgent);
    var _nowtime = new Date();
    var _os = md.os();
    var _device = md.phone();
    var _browser = md.userAgent();

    lobsterh5.GETAPI('tsForm.getformobject', { formid: formid }).then(retdata => {
        var pagejson = JSON.parse(retdata.formobj.Viewschema);
        pagejson.body.forEach(item => {
            if (item.type == "form") {
                item.api = {};
                item.api.method = "post";
                item.api.dataType = "json";
                item.api.headers = lobstercommon.GetHeaderData();
                item.api.url = "/lowcode/?view=amis&fun=tsForm.saveformdata&debug=false";
                item.redirect = "";
            }
        });


        //显示表单
        let amis = amisRequire('amis/embed');
        // 通过替换下面这个配置来生成不同页面
        let amisScoped = amis.embed('#root', pagejson, {}, {
            // 可以不传，用来实现 ajax 请求
            fetcher: (config) => {

                var _cip = typeof (returnCitySN)=='undefined' ? "":returnCitySN["cip"];
                var _cname = typeof (returnCitySN)=='undefined' ?"": returnCitySN["cname"];

                var _endtime = new Date();
                var otherinfo = { "formid": formid, "timelong": Math.floor((_endtime - _nowtime) / 1000), "os": _os, "device": _device, "browser": _browser, "cip": _cip, "cname": _cname };
                var formdata = JSON.stringify({ formvalue: JSON.parse(config.data), otherinfo: otherinfo });

                //var _config=config;
                return new Promise((resolve, reject) => {
                    $.ajax({
                        url: config.url,
                        type: config.method,
                        data: formdata,
                        headers: config.headers,
                        contentType: "application/json",
                        dataType: config.dataType,
                        success: function (res) {
                            console.log(res);
                            var fetcherResult = { data: {}, status: 0, headers: {} };
                            fetcherResult.data = res;
                            fetcherResult.status = res.status;
                            resolve(fetcherResult);

                            if (res.status === 0) {
                                //指定了提交成功的地址
                                if (backurl != "") {
                                    if (backurl.indexOf('?') > 0) {
                                        window.location.replace(backurl + "&id=" + res.data.result.Id);
                                    } else {
                                        window.location.replace(backurl + "?id=" + res.data.result.Id);
                                    }
                                } else {
                                    window.location.replace("formsucce.html?appid=" + lobstercommon.GetAppId() + "&id=" + res.data.result.Id);
                                }
                            }
                        }
                    });
                })
            }
            // 可以不传，全局 api 适配器。
            // 另外在 amis 配置项中的 api 也可以配置适配器，针对某个特定接口单独处理。
            // responseAdaptor(api, response, query, request) {
            //     return response;
            // }
        });
    })
});
